require "base_def"
local skynet = require "skynet"
local cluster = require "skynet.cluster"
--业务文件包含
local hand_login = require "hand_login"
local hand_channel = require "hand_channel"
local handle_config = require "handle_config"

--
local mysql_manager = {}

--[[
function mysql_manager:Call(callStr)
    --log.info("cluster call mysql_manager callstr = ", callStr)
    local ok, res = self.hand_mysql_connect:Call(callStr)
    --log.info("mysql_manager res=", res)
    return ok, res
end
--]]

--消息分发（单节点和集群都找该管理服务首先查到对应的查询函数所在文件[为了服务模块化]）
function mysql_manager:DispatchMsg(cmd, ...)
    if hand_login[cmd] then
        return hand_login[cmd](...)
    elseif hand_channel[cmd] then
        return hand_channel[cmd](...)
    elseif handle_config[cmd] then
        return handle_config[cmd](...)
    else 
        log.error("mysql_manager:DispatchMsg no cmd =", cmd)
    end
end

function mysql_manager:Init()
    log.info("---------------------mysql_manager init---------------------")
    --管理节点ID
    self.node = g_manager_node.mysql
    skynet.register(".mysql")
    --集群模式下才需要mysql节点管理服务
    if FRAME_CALL_TYPE==__CALL_TYPE_CLUSTER__ then
        cluster.open "mysql"
    end

    --redis库查询工具服务
    self.mysqlTool = skynet.uniqueservice("mysql_tools")

    --开启短信服务
    local short_message = skynet.uniqueservice("short_message")
end

skynet.start(function()
    skynet.dispatch("lua", function(session, address, cmd, ...)
        log.info("mysql_manager DispatchMsg cmd = ", cmd)
        local f = mysql_manager["DispatchMsg"]
        if f then
            skynet.ret(skynet.pack(f(mysql_manager, cmd, ...)))
        else
            skynet.error(string.format("Unknown command %s", tostring(cmd)))
        end
    end)

    mysql_manager:Init()
end)



return mysql_manager
